var CommonUtil = new CommonUtil();

$(function() {
	findItemsById();
});

function toItemList() {
	location.href = CommonUtil.getRootPath() + '/sm-demo/page/list/itemList.html';
}

function findItemsById() {
	var id = CommonUtil.getArgs('id')['id'];
    $.ajax({
        cache: true,
        type: 'POST',
		url : CommonUtil.getRootPath() + '/sm-demo/findItemsById.action',
        data: JSON.stringify({
        	id: Number(id)
        }),
        dataType: 'JSON',
        contentType: 'application/json',
        async: true,
        beforeSend: function() {},
        error: function (data) {
        	console.log(data);
        },
        success: function (data) {
//        	alert(JSON.stringify(data));
        	$('#j_itemName').val(data.name);
        	$('#j_itemPrice').val(data.price);
        	$('#j_itemTime').val(data.createtime);
        	$('#j_itemDetails').val(data.detail);
        	if (data.pic) {
        		$('#j_img').attr('src', data.pic);
        	}
        }
        });
}


function addAndUpdataItem() {
	var id = CommonUtil.getArgs('id')['id'];
	
    $.ajax({
        cache: true,
        type: 'POST',
		url : CommonUtil.getRootPath() + '/sm-demo/insertAndUpdataItem.action',
        data: JSON.stringify({
        	id: Number(id),
        	name: $('#j_itemName').val(),
        	price: $('#j_itemPrice').val(),
        	createtime: $('#j_itemTime').val(),
        	detail: $('#j_itemDetails').val(),
        	pic: '',
        }),
        dataType: 'JSON',
        contentType: 'application/json',
        async: true,
        beforeSend: function() {},
        error: function (data) {
        	console.log(data);
        },
        success: function (data) {
//        	alert(JSON.stringify(data));
        	if (data.status === 0) {
        		toItemList();
        	} else {
        		alert(data.msg);
        	}
        }
        });
}